import requests
import json

class King(object):
    def __init__(self,word):
        self.url = "http://fy.iciba.com/ajax.php?a=fy"
        self.word = word
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        }
        self.post_data = {
            "f": "auto",
            "t": "auto",
            "w": self.word
        }
    def parse_data(self,data):
        # 将json数据转换成python字典
        dict_data = json.loads(data)
        # 从字典中抽取翻译结果
        try:
            print(dict_data['content']['out'])
        except:
            print(dict_data['content']['word_mean'][0])
    def run(self):
        # url
        # headers
        # post——data
        # 发送请求
        data = self.get_data()
        # 解析
        self.parse_data(data)
if __name__ == '__main__':
        # king = King("人生苦短，及时行乐")
        king = King("China")
        king.run()
        # python标准库有很多有用的方法，每天看一个标准库的使用